//
//  HomeViewController.m
//  MediaResourceSystem
//
//  Created by 智障 on 2018/11/5.
//  Copyright © 2018 zerod. All rights reserved.
//

#import "HomeViewController.h"

#import "SettingViewController.h"
#import "AddLiving_ViewController.h"

#import "LivingDetail_ViewController.h"

#import "Home_NavView.h"
#import "HomeLivingList_TableViewCell.h"
#import "LoadDataStatusView.h"

#import "LivingListModel.h"
#import "LiveDetailModel.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView    *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic ,strong) Home_NavView   *navView;
@property (nonatomic ,strong) LoadDataStatusView        *dataStatusView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.navView];
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.dataStatusView];

    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestListMoreRefresh:NO];

    [self.navView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(SafeAreaTopHeight);
        make.width.mas_equalTo(ScrW);
    }];
}
-(UITableView*)tableView{
    if(!_tableView){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, ScrH - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.showsHorizontalScrollIndicator=NO;
        _tableView.backgroundColor = RGB(239, 239, 239);
        _tableView.separatorStyle = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"HomeLivingList_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        WS(weakSelf)
        _tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf requestListMoreRefresh:NO];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf requestListMoreRefresh:YES];
        }];
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeLivingList_TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[HomeLivingList_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellAccessoryNone;
    LivingListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
     WS(weakSelf)
    cell.modifyClick = ^{
        [weakSelf pusfModifyLivingWith:model];
        
    };
   
    cell.actionClick = ^{
        [weakSelf pushDetailLivingVCWith:model];
    };
    return cell;
}
-  (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 123.f;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)pushDetailLivingVCWith:(LivingListModel *)model{
    if (model.rtmpPublishurl.length == 0) {
        SHOWERROR(@"推流地址为空") return;
    }
//    if (model.streamStatus == 1 && model.status == 1) {//可用，且直播中
        LivingDetail_ViewController *vc = [LivingDetail_ViewController new];
        vc.pushUrl = model.rtmpPublishurl;
        [self.navigationController pushViewController:vc animated:NO];
//    }
}
- (void)pusfModifyLivingWith:(LivingListModel *)model{
    NSString*url=[NSString stringWithFormat:@"%@/api/live/detail?projectId=%@",URL_HOST,model.id];
    [SendRequest getWithURL:url params:nil withCookie:YES success:^(id response) {
        int code = [response[@"code"] intValue];
        NSString *message = response[@"msg"];
        if(code==0){
            LiveDetailModel *detailModel = [LiveDetailModel mj_objectWithKeyValues:response[@"data"][@"topicList"]];
            AddLiving_ViewController *vc = [AddLiving_ViewController new];
            vc.isModify = YES;
            vc.model = detailModel;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            SHOWERROR(message)
        }
    } fail:^(NSError *error) {
        SHOWERROR(showNetworkError)
    }];


}
- (Home_NavView *)navView{
    if (!_navView) {
        _navView = [Base_loadXibView loadFromXib:@"Home_NavView" withCls:[Home_NavView class]];
        [_navView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SafeAreaTopHeight)];
    
        WS(weakSelf)
        _navView.setClick = ^{
            [weakSelf.navigationController pushViewController:[SettingViewController new] animated:YES];
        };
        _navView.addClick = ^{
            [weakSelf.navigationController pushViewController:[AddLiving_ViewController new] animated:YES];
        };
    }
    return _navView;
}
- (LoadDataStatusView *)dataStatusView{
    if (!_dataStatusView) {
        _dataStatusView = [[LoadDataStatusView alloc]initWithFrame:self.tableView.bounds];
        _dataStatusView.hidden = YES;
    }
    return _dataStatusView;
}
static int page = 1;
- (void)requestListMoreRefresh:(BOOL)isMore{
    if (isMore) {
        page += 1;
    }else{
        page = 1;
        self.dataArray = [NSMutableArray array];
    }
    NSString*url=[NSString stringWithFormat:@"%@/api/live/app/list?page=%d&pageSize=10&videoType=0",URL_HOST,page];
    [SendRequest postWithURL:url params:nil withCookie:YES success:^(id response) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        int code = [response[@"code"] intValue];
        NSString *message = response[@"msg"];
        if(code==0){
            
            NSArray *dataAry = [[[response objectForKey:@"data"] objectForKey:@"topicLists"] mj_JSONObject];
//            NSLog(@"%@",dataAry);
            NSMutableArray *modelAry = [LivingListModel mj_objectArrayWithKeyValuesArray:dataAry];
            // 没有新数据了
            if([modelAry isKindOfClass:[NSNull class]]||modelAry.count==0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.dataArray addObjectsFromArray:modelAry];
            }
        }else{
            SHOWERROR(message)
        }
        if (self.dataArray.count == 0) {
            [self.dataStatusView notHaveData];
        }else{
            self.dataStatusView.hidden = YES;
        }
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        SHOWERROR(showNetworkError)
    }];
}
@end
